package com.tongtech.system.controller;

import com.tongtech.common.core.constant.UserConstants;
import com.tongtech.common.core.utils.StringUtils;
import com.tongtech.common.core.web.controller.BaseController;
import com.tongtech.common.core.web.domain.AjaxResult;
import com.tongtech.common.log.annotation.Log;
import com.tongtech.common.log.enums.BusinessType;
import com.tongtech.common.security.annotation.RequiresPermissions;
import com.tongtech.common.security.utils.SecurityUtils;
import com.tongtech.system.domain.Label;
import com.tongtech.system.service.ILabelService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 标签信息
 *
 * @author xingyc
 */
@RestController
@RequestMapping("/label")
public class LabelController extends BaseController
{
    @Autowired
    private ILabelService labelService;

    /**
     * 获取标签列表
     */
    @RequiresPermissions("basicdata:label:list")
    @GetMapping("/list")
    public AjaxResult list(Label label)
    {
        List<Label> labels = labelService.selectLabelList(label);
        return success(labels);
    }

    /**
     * 查询标签列表（排除节点）
     */
    @RequiresPermissions("basicdata:label:list")
    @GetMapping("/list/exclude/{labelId}")
    public AjaxResult excludeChild(@PathVariable(value = "labelId", required = false) Long labelId)
    {
        List<Label> labels = labelService.selectLabelList(new Label());
        labels.removeIf(p -> p.getLabelId().intValue() == labelId || ArrayUtils.contains(StringUtils.split(p.getAncestors(), ","), labelId + ""));
        return success(labels);
    }

    /**
     * 根据标签编号获取详细信息
     */
    @RequiresPermissions("basicdata:label:query")
    @GetMapping(value = "/{labelId}")
    public AjaxResult getInfo(@PathVariable Long labelId)
    {
        return success(labelService.selectLabelById(labelId));
    }

    /**
     * 新增标签
     */
    @RequiresPermissions("basicdata:label:add")
    @Log(title = "标签管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Label label)
    {
        if (!labelService.checkLabelNameUnique(label))
        {
            return error("新增标签'" + label.getLabelName() + "'失败，标签名称已存在");
        }
        label.setCreateBy(SecurityUtils.getUsername());
        return toAjax(labelService.insertLabel(label));
    }

    /**
     * 修改标签
     */
    @RequiresPermissions("basicdata:label:edit")
    @Log(title = "标签管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Label dept)
    {
        Long deptId = dept.getLabelId();
        if (!labelService.checkLabelNameUnique(dept))
        {
            return error("修改标签'" + dept.getLabelName() + "'失败，标签名称已存在");
        }
        else if (dept.getParentId().equals(deptId))
        {
            return error("修改标签'" + dept.getLabelName() + "'失败，上级标签不能是自己");
        }
        else if (StringUtils.equals(UserConstants.DEPT_DISABLE, dept.getStatus()) && labelService.selectNormalChildrenLabelById(deptId) > 0)
        {
            return error("该标签包含未停用的子标签！");
        }
        dept.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(labelService.updateLabel(dept));
    }

    /**
     * 删除标签
     */
    @RequiresPermissions("basicdata:label:remove")
    @Log(title = "标签管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deptId}")
    public AjaxResult remove(@PathVariable Long deptId)
    {
        if (labelService.hasChildByLabelId(deptId))
        {
            return warn("存在下级标签,不允许删除");
        }
        return toAjax(labelService.deleteLabelById(deptId));
    }
}
